(function() {
    $.fn.extend({
        register() {
            var seft = this
            var targer = false //默认都合法
            var targerm = false
            var Ovalue = ""
            var Ovalue2 = ""

            this.find(".regi_form_input").children("input").on("input", function() {

                //合法的电话号码
                var phone = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/
                Ovalue = seft.find(".regi_form_input").children("input").val()
                var ptn1 = phone.test(Ovalue)
                if (Ovalue == "") {
                    return
                }


                // console.log("1w11w1" + ptn1)
                if (ptn1 == true) {
                    seft.find(".area").text("")
                    targer = true
                } else {
                    seft.find(".area").text(" ! !输入号码不合法").css("color", "red")
                    targer = false
                    return
                }
            })

            this.find(".regi_form_input_two").children("input").on("input", function() {

                var pattern = /^[a-zA-Z_]\w{5,14}$/
                Ovalue2 = seft.find(".regi_form_input_two").children("input").val()
                var ptn2 = pattern.test(Ovalue2)
                    // console.log("squhsqsq" + ptn2)
                if (Ovalue2 == "") {
                    return
                }
                if (ptn2 == true) {
                    seft.find(".area").text("")
                    targerm = true
                        // console.log(111)
                } else {
                    seft.find(".area").text("! !输入密码不合法").css("color", "red")
                    targerm = false
                        // console.log(targerm)
                    return
                }
            })

            code_draw();
            // 点击后刷新验证码
            $("#canvas").on('click', function() {
                code_draw();
            })

            var val = ""
            var num = ""
            this.find(".regi_form_input_three-input").on("input", function() {
                // 将输入的内容转为大写，可通过这步进行大小写验证
                val = $(".regi_form_input_three-input").val().toLowerCase();

                // 获取生成验证码值
                num = $('#canvas').attr('data-code');
                console.log(num)
                if (val == '') {
                    return
                } else if (val != num) {
                    seft.find(".area").text("! !请输入正确的验证码").css("color", "red")
                } else {
                    seft.find(".area").text("")

                }

            })


            this.find("button").on("click", function() {
                console.log(num)
                if (targer == false || targerm == false || Ovalue == "" || Ovalue2 == "") {
                    layer.alert("请输入正确的号码/邮箱和密码", { icom: 2, title: "注册失败" })
                    return
                } else if (val != num || val == "") {
                    layer.alert("请输入正确的验证码", { icom: 2, title: "注册失败" })
                    return
                } else {
                    var OuesrsList = {
                        name: seft.find(".regi_form_input").children("input").val(),
                        pwd: seft.find(".regi_form_input_two").children("input").val()
                    }
                    console.log(OuesrsList)

                    $.ajax({
                        url: "http://127.0.0.1:8080/reginlogin/regin ",
                        type: "post",
                        data: OuesrsList,
                        async: true
                    }).then(function(res) {
                        var { msg, code } = res
                        // layer.alert(msg, { icon: 2, title: "注册失败" })
                        alert(msg)
                            // if (code == 200) {
                            //     //先获取cookie里面的数据
                            //     var OcookieList = JSON.parse(Cookies.get("CookieList") || '[]')
                            //     console.log(OcookieList)

                        //     var flag = true //默认未注册
                        //     console.log(flag)
                        //     for (var i = 0; i < OcookieList.length; i++) {
                        //         if (OcookieList[i].name == OuesrsList.name) {
                        //             flag = false //说明已注册
                        //             layer.alert("该用户已注册", { icon: 2, title: "注册失败" })
                        //             return
                        //         }
                        //     }

                        //     if (flag) {
                        //         OcookieList.push(OuesrsList)
                        //             //将数据从新添加到cookie中
                        //         Cookies.set("CookieList", JSON.stringify(OcookieList), { expires: 10 })
                        //         layer.alert("注册成功", {
                        //             icon: 1,
                        //             title: "注册成功"
                        //         })
                        //         return
                        //     } else {
                        //         layer.alert("请勿重复注册", { icom: 2, title: "注册失败" })
                        //     }
                        // }
                    })

                }
            })
        }


    })

    $(function() {
        $(".Oinput").register()
    })


})()